package ph.com.gs3.formalistics.fragments.views;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.adapters.DocumentListItemViewAdapter;
import ph.com.gs3.formalistics.interfaces.view.UserActionListItemInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class DocumentListViewFragment extends Fragment {

	private TextView tvHeader;

	private String header;

	private ListView lvDocumentList;
	private List<UserActionListItemInterface> list;

	private DocumentListItemViewAdapter adapter;

	public static DocumentListViewFragment createInstance(String header,
			List<UserActionListItemInterface> list) {
		DocumentListViewFragment instance = new DocumentListViewFragment();
		instance.header = header;
		instance.list = list;

		return instance;

	}

	public void setDataList(List<UserActionListItemInterface> list) {
		if (this.list == null) {
			this.list = new ArrayList<UserActionListItemInterface>();
		} else {
			this.list.clear();
		}

		this.list.addAll(list);
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_document_list,
				container, false);

		initializeViews(rootView);
		return rootView;
	}

	private void initializeViews(View rootView) {

		tvHeader = (TextView) rootView.findViewById(R.id.DL_tvHeader);
		
		tvHeader.setText(header);

		lvDocumentList = (ListView) rootView
				.findViewById(R.id.DL_lvDocumentList);

		adapter = new DocumentListItemViewAdapter(getActivity(), list);
		lvDocumentList.setAdapter(adapter);
	}

}
